package com.ruoyi.common.tools.excel.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.tools.constant.ERecordConstant;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.erecord.employee.domain.bo.EmployeeBo;
import com.ruoyi.erecord.employee.domain.vo.EmployeeVo;
import com.ruoyi.erecord.employee.service.impl.EmployeeServiceImpl;
import com.ruoyi.standingbook.toilet.domain.vo.ToiletImportDetailVo;
import com.ruoyi.system.service.impl.SysUserServiceImpl;

import java.util.ArrayList;
import java.util.List;

public class ToiletExcelListener extends MyExcelListener<ToiletImportDetailVo> {

    private DictService dictService = SpringUtils.getBean(DictService.class);

    private EmployeeServiceImpl employeeServiceImpl = SpringUtils.getBean(EmployeeServiceImpl.class);

    private String batchNo = null;

    public ToiletExcelListener(String batchNo) {
        this.batchNo = batchNo;
    }

    @Override
    public void doValidate(ToiletImportDetailVo data, AnalysisContext context) {
        // 1.设置批次号
        // 2.校验数据 与 匹配数据字典数据 设置对应字段
        // 3.设置 处理状态 与 处理信息
        List<String> validateList = new ArrayList<>();

        data.setBatchNo(this.batchNo);

        if (!StringUtils.isEmpty(data.getItemName())) {
            String val = this.dictService.getDictValue("erecord_item_name_list", data.getItemName());
            if (val == "") {
                validateList.add("[ 项目名称 ] 匹配不到数据");
            } else {
                data.setItemName(val);
            }
        } else {
            validateList.add("[ 项目名称 ] 为空");
        }

        if (data.getDate() == null) {
            validateList.add("[ 日期 ] 为空");
        }


        if (!StringUtils.isEmpty(data.getCommunity())) {
            String val = this.dictService.getDictValue("erecord_community", data.getCommunity());
            if (val == "") {
                validateList.add("[ 社区 ] 匹配不到数据");
            } else {
                data.setCommunity(val);
            }
        } else {
            validateList.add("[ 社区 ] 为空");
        }

        if (StringUtils.isEmpty(data.getToiletName())) {
            validateList.add("[ 公厕名称 ] 为空");
        }

        if (data.getActualFrequency() == null) {
            validateList.add("[ 清洁频率（次） ] 为空");
        }

        if (data.getCount() == null) {
            validateList.add("[ 厕位 为空");
        }

        if (StringUtils.isEmpty(data.getStatus())) {
            validateList.add("[ 完成情况 为空");
        }

        //匹配保洁人员
        EmployeeBo bo=new EmployeeBo();
        bo.setEmployeeName(data.getCleaningUser().trim());
        List<EmployeeVo> employeeList = employeeServiceImpl.queryList(bo);
        if (CollectionUtils.isNotEmpty(employeeList)) {
            if (employeeList.size() > 0) {
                for (EmployeeVo employeeVo : employeeList) {
                    if (employeeVo.getEmployeeName().equals(data.getCleaningUser().trim()) && employeeVo.getPhone().equals(data.getPhoneNumber().trim())) {
                        data.setCleaningUserId(employeeVo.getId());
                        break;
                    }
                }
            } else {
                data.setCleaningUserId(employeeList.get(0).getId());
            }
        } else {
            validateList.add(data.getCleaningUser() + "保洁人员在系统中不存在");
        }

//        if (StrUtil.isEmpty(data.getClearingRoute()) || data.getClearingRoute().trim().equals("/")) {
//            data.setClearingRoute("-");
//        }


        Integer col = this.getHeadColIndex("现场清洁前图片");
        if (null != col) {
            data.setPic1(this.getPicOssIdOnMap(row, col));
        }

//        col = this.getHeadColIndex("现场清洁前图片2");
//        if (null != col) {
//            data.setPic2(this.getPicOssIdOnMap(row, col));
//        }

        col = this.getHeadColIndex("现场清洁中图片");
        if (null != col) {
            data.setPic3(this.getPicOssIdOnMap(row, col));
        }

//        col = this.getHeadColIndex("现场清洁中图片2");
//        if (null != col) {
//            data.setPic4(this.getPicOssIdOnMap(row, col));
//        }

        col = this.getHeadColIndex("现场清洁后图片");
        if (null != col) {
            data.setPic5(this.getPicOssIdOnMap(row, col));
        }

//        col = this.getHeadColIndex("现场清洁后图片2");
//        if (null != col) {
//            data.setPic6(this.getPicOssIdOnMap(row, col));
//        }


        if (!validateList.isEmpty()) {
            String errMsg = String.join(" ，", validateList);
            excelResult.getValidateList().add(errMsg);

            data.setProcessMsg(errMsg);
            data.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
        } else {
            data.setProcessMsg("成功");
            data.setProcessStatus(ERecordConstant.COMMON_STATUS_SUCCESS);
        }
    }

    @Override
    protected void doOnExceptionProcess(String errorMsg) {
        ToiletImportDetailVo vo = new ToiletImportDetailVo();
        vo.setBatchNo(this.batchNo);
        vo.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
        vo.setProcessMsg(errorMsg);
        this.excelResult.getList().add(vo);
    }

}
